package app.itgungnir.compose.wan.widgets

import android.content.res.Configuration
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme

@Composable
fun WanHeader(
    modifier: Modifier = Modifier,
    content: String? = null,
    color: Color = MaterialTheme.colors.onBackground,
    style: TextStyle = MaterialTheme.typography.h6.copy(fontWeight = FontWeight.ExtraBold),
    maxLines: Int = 1,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified
) {
    content?.takeIf { it.isNotEmpty() }?.let { text ->
        Text(
            modifier = modifier,
            text = text,
            color = color,
            style = style,
            maxLines = maxLines,
            overflow = TextOverflow.Ellipsis,
            textAlign = textAlign,
            lineHeight = lineHeight
        )
    }
}

@Composable
fun WanTitle(
    modifier: Modifier = Modifier,
    content: String? = null,
    color: Color = MaterialTheme.colors.onBackground,
    style: TextStyle = MaterialTheme.typography.subtitle1.copy(fontWeight = FontWeight.SemiBold),
    maxLines: Int = 2,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified
) {
    content?.takeIf { it.isNotEmpty() }?.let { text ->
        Text(
            modifier = modifier,
            text = text,
            color = color,
            style = style,
            maxLines = maxLines,
            overflow = TextOverflow.Ellipsis,
            textAlign = textAlign,
            lineHeight = lineHeight
        )
    }
}

@Composable
fun WanDescription(
    modifier: Modifier = Modifier,
    content: String? = null,
    color: Color = MaterialTheme.colors.onBackground,
    style: TextStyle = MaterialTheme.typography.subtitle2,
    maxLines: Int = 3,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified
) {
    content?.takeIf { it.isNotEmpty() }?.let { text ->
        Text(
            modifier = modifier,
            text = text,
            color = color,
            style = style,
            maxLines = maxLines,
            overflow = TextOverflow.Ellipsis,
            textAlign = textAlign,
            lineHeight = lineHeight
        )
    }
}

@Composable
fun WanError(
    modifier: Modifier = Modifier,
    contentState: State<Throwable?>? = null,
    color: Color = MaterialTheme.colors.error,
    style: TextStyle = MaterialTheme.typography.subtitle2,
    maxLines: Int = 3,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified
) {
    WanDescription(
        modifier = modifier,
        content = contentState?.value?.message,
        color = color,
        style = style,
        maxLines = maxLines,
        textAlign = textAlign,
        lineHeight = lineHeight
    )
}

@Composable
fun WanInfo(
    modifier: Modifier = Modifier,
    content: String? = null,
    prefix: String = "",
    color: Color = MaterialTheme.colors.onBackground.copy(alpha = 0.6F),
    style: TextStyle = MaterialTheme.typography.caption,
    maxLines: Int = 1,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified
) {
    content?.takeIf { it.isNotEmpty() }?.let { text ->
        Text(
            modifier = modifier,
            text = "$prefix$text",
            color = color,
            style = style,
            maxLines = maxLines,
            overflow = TextOverflow.Ellipsis,
            textAlign = textAlign,
            lineHeight = lineHeight
        )
    }
}

@Preview(name = "WanText light mode", showBackground = true)
@Preview(name = "WanText dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
private fun WanTextPreview() {
    WanComposePreviewTheme {
        Column(modifier = Modifier.fillMaxWidth()) {
            WanHeader(content = "头部头部头部头部头部")
            WanTitle(content = "标题标题标题标题标题")
            WanDescription(content = "内容内容内容内容内容")
            WanInfo(content = "信息信息信息信息信息", prefix = "信息：")
        }
    }
}
